import Ember from 'ember';
import BaseItem from '../ui/base-ui-item';
import Changeset from 'ember-changeset';
import CustomerValidations from '../../validations/customer-live-info';
import lookupValidator from 'ember-changeset-validations';

export default BaseItem.extend(CustomerValidations, {
    constants: Constants,
    statusService: Ember.inject.service("current-status"),
    store: Ember.inject.service("store"),
    dateService: Ember.inject.service("date-service"),
    pathConfiger: Ember.inject.service("path-configer"),
    //delFlag:true,
    customerModel: Ember.computed("customerInComp", function() {
        var model = this.get("customerInComp");
        console.log("model customerInComp", model);
        if (!model) {
            return null;
        }
        return new Changeset(model, lookupValidator(CustomerValidations), CustomerValidations);
    }),
    refreshStaffList: function() {
        var route = App.lookup('route:business.mainpage.customer-service');
        //route.refresh();
        App.lookup('controller:business.mainpage').refreshPage(route);
    },
    detailModify: Ember.computed("addMode", function() {
        var addMode = this.get("addMode");
        if (addMode) {
            return true;
        } else {
            return false;
        }
    }),
    // init() {
    //     this._super(...arguments);
    //     let model = this.get("store").createRecord('customer', {});
    //     // this.set("customerInComp",model);
    //     this.customerInComp = model;
    // },
    defaultBed: Ember.computed('customerInComp.bed', function() {
        console.log("customerInComp.bed", this.get('customerInComp.bed'));
        //this.set('bedListFirst',bedList.get('firstObject'));
        //if (this.get('customerInComp.bed') && this.get('customerInComp.bed').content) {
        return this.get('customerInComp.bed');
        //}
        //return this.get('bedListFirst');
    }),
    actions: {
      dpShowAction(e){

      },
        invalid() {
            //alert("invalid");
        },
        //修改
        detailModifyClick: function() {
            this.set('detailModify', true);
        },
        //保存按钮
        detailSaveClick: function() {
            var _self = this;
            var customerModel = this.get("customerModel");
            var mainpageController = App.lookup('controller:business.mainpage');
            console.log("++++++++++customerModel+++++++++++", this.get("customerModel"));
            customerModel.validate().then(function() {
                //alert("save   out");
                if (customerModel.get('errors.length') === 0) {
                    App.lookup('controller:business.mainpage').openPopTip("正在保存");
                    //出生日期转化为时间戳
                    // var birthdayTime = _self.get("customerInComp.birthdayString");
                    // console.log("++++++++++birthdayTime++++++++++++", birthdayTime);
                    // var birthdayDate = _self.get("dateService").timeStringToTimestamp(birthdayTime);
                    // console.log("++++++++++birthdayDate++++++++++++", birthdayDate);
                    // customerModel.set("birthday", birthdayDate);
                    //入住开始日期转化为时间戳
                    // var checkInTime = _self.get("customerInComp.checkInDateString");
                    // var checkInStamp = _self.get("dateService").timeStringToTimestamp(checkInTime);
                    // customerModel.set("checkInDate", checkInStamp);
                    //入住截止时间转化为时间戳
                    // var checkOutTime = _self.get("customerInComp.checkOutDateString");
                    // var checkOutStamp = _self.get("dateService").timeStringToTimestamp(checkOutTime);
                    // customerModel.set("checkOutDate", checkOutStamp);
                    customerModel.save().then(function() {
                        App.lookup('controller:business.mainpage').showPopTip("保存成功");
                        if (_self.get('addMode')) {
                            alert("添加");
                            mainpageController.switchMainPage('customer-service', {});
                        }
                        _self.set('detailModify', false);
                    });
                } else {
                    customerModel.set("validFlag", Math.random());
                    alert("校验失败");
                }
            });
        },
        //取消按钮
        cancelClick: function() {
            var mainpageController = App.lookup('controller:business.mainpage');
            this.set('detailModify', false);
            //alert("详情");
            if (!this.get('addMode')) {
                this.get("customerInComp").rollbackAttributes();
                this.set("customerModel", new Changeset(this.get("customerInComp"), lookupValidator(CustomerValidations), CustomerValidations));
            }
            if (this.get("addMode")) {
                mainpageController.switchMainPage('customer-service', {
                    //flag: 'edit-add'
                });
            }
        },

        //删除按钮
        delById: function(customerInComp) {
            //this.set('showpopInvitePassModal', true);
            var _self = this;
            App.lookup('controller:business.mainpage').showConfirm("是否确定删除此入住信息", function() {
                App.lookup('controller:business.mainpage').openPopTip("正在删除");
                customerInComp.set("delStatus", 1);
                customerInComp.save().then(function() {
                    App.lookup('controller:business.mainpage').showPopTip("删除成功");
                    var mainpageController = App.lookup('controller:business.mainpage');
                    mainpageController.switchMainPage('customer-service');
                });
            });
        },
       diningSelect: function(diningDict) {
            this.get("customerInComp").set("diningStandard", diningDict);
        }, //餐饮标准字典
        nursingSelect: function(nursingDict) {
            this.get("customerInComp").set("nursingGrade", nursingDict);
        }, //护理等级字典
        selectParent(bed) {
            //this.set("customerInComp").set("bed",bed);
            this.get('customerModel').set("bed", bed);
            this.set("defaultBed", bed);
        }, //床位信息

        changeCheckInDateAction(date) {
            console.log("changeDateAction in,date", date);
            var stamp = this.get("dateService").getLastSecondStampOfDay(date);
            console.log("stamp is:" + stamp);
            this.set("customerModel.checkInDate", stamp);
        },
        changeCheckOutDateAction(date) {
            var stamp = this.get("dateService").getLastSecondStampOfDay(date);
            this.set("customerModel.checkOutDate", stamp);
        },
    }
});
